require 'posix-spawn'

module ClosureProject
  module CommandRunner
    def self.run_and_log( cmd, options = {} )
      result = POSIX::Spawn::Child.new( cmd );
      className = options[:class_name] ? (options[:class_name] + ':') : '';
      
      if result.status.exitstatus != 0
        unless options[:silent]
          warn "#{className} command: #{cmd}"
          warn result.err if result.err != ''
        end
        raise "#{className} Error"
      end
        
      if options[:verbose]
        puts "Command: #{cmd}"
        puts result.err if result.err != ''
      end
      
      result
    end
  end
end
